package com.xueyi.game.backend.controller;

import com.xueyi.core.base.BaseBackController;
import com.xueyi.core.base.BaseResponse;
import com.xueyi.core.base.TableData;
import com.xueyi.core.expection.BusinessException;
import com.xueyi.game.backend.entity.dto.BaseUserLoginDto;
import com.xueyi.game.backend.entity.dto.FindUserLogByUserIdDto;
import com.xueyi.game.backend.entity.dto.SaveBaseUserDto;
import com.xueyi.game.backend.entity.dto.UserTableDto;
import com.xueyi.game.backend.entity.po.BaseUser;
import com.xueyi.game.backend.entity.vo.UserLoginVo;
import com.xueyi.game.backend.service.IBackendAccountService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 描述 用户的Controller
 *
 * @auther 俞烨
 * @date 2022-11-08 21
 */
@RestController
@AllArgsConstructor
@Validated
public class UserController extends BaseBackController {

    private final IBackendAccountService iBackendAccountService;

    /**
     * 用户登录接口
     *
     * @param baseUserLoginReq
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<UserLoginVo<?>> login(@Validated @RequestBody BaseUserLoginDto baseUserLoginReq,
                                              HttpServletRequest request) throws BusinessException {
        return BaseResponse.success(iBackendAccountService.userLogin(baseUserLoginReq, request));
    }

    /**
     * 新增新的用户信息
     *
     * @param saveBaseUser
     * @return
     */
    @PostMapping("/save/user")
    public BaseResponse<Boolean> saveUser(@Validated @RequestBody SaveBaseUserDto saveBaseUser) throws BusinessException {
        iBackendAccountService.save(saveBaseUser);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 通过Id查询登录的用户信息
     *
     * @param userId
     * @return
     * @throws BusinessException
     */
    @GetMapping("/findUserDetailById")
    public BaseResponse<BaseUser> findUserDetailById(@NotNull Integer userId) throws BusinessException {
        return BaseResponse.success(iBackendAccountService.findUserById(userId));
    }

    /**
     * 修改用户的信息
     *
     * @param saveBaseUser
     * @return
     */
    @PostMapping("/edit/user")
    public BaseResponse<Boolean> editUser(@Validated @RequestBody SaveBaseUserDto saveBaseUser) throws BusinessException {
        iBackendAccountService.edit(saveBaseUser);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 删除或者恢复用户
     *
     * @return
     */
    @PostMapping("/delOrRecover")
    public BaseResponse<Boolean> delOrRecover(@NotNull Integer userId,
                                              @NotNull Integer status) {
        iBackendAccountService.delOrRecover(userId, status);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 查询用户的列表数据
     *
     * @param userTableReq
     * @return
     */
    @GetMapping("/findUserList")
    public BaseResponse<TableData<?>> findUserList(UserTableDto userTableReq) {
        return BaseResponse.success(iBackendAccountService.findUserList(userTableReq));
    }

    /**
     * 查询用户的菜单列表
     *
     * @return
     */
    @GetMapping("/findUserMenu")
    public BaseResponse<?> findUserMenu() {
        return BaseResponse.success(iBackendAccountService.findUserMenuVo());
    }

    /**
     * 获取用户操作日志lieb
     *
     * @param findUserLogByUserIdReq
     * @return
     * @author hxs
     */
    @GetMapping("/findUserLogByUserId")
    public BaseResponse<?> findUserLogByUserId(@Validated FindUserLogByUserIdDto findUserLogByUserIdReq) {
        return BaseResponse.success(iBackendAccountService.findUserLogByUserId(findUserLogByUserIdReq));
    }

}
